//
//  BLTabBarViewController.m
//  新浪微博
//
//  Created by hbl on 16/2/14.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLTabBarViewController.h"
#import "BLNavigationViewController.h"
#import "HomeTableViewController.h"
#import "MessageTableViewController.h"
#import "MeTableViewController.h"
#import "DiscoverTableViewController.h"
#import "BLTabBar.h"
#import "SendStatuesViewController.h"
#import "MBProgressHUD+MJ.h"
@interface BLTabBarViewController ()<BLTabBarDelegate,SendStatuesViewDelegate>
@property (nonatomic, weak)BLTabBar *customTabBar;
@end

@implementation BLTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTheTabBar];
    [self setTheChildViewController];
 }

- (void)setTheTabBar
{
    BLTabBar *customTabBar = [[BLTabBar alloc]init];
    customTabBar.delegate = self;
    customTabBar.frame = self.tabBar.bounds;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar = customTabBar;
}

- (void)setTheChildViewController
{
    //home
    HomeTableViewController *home = [[HomeTableViewController alloc]init];
    home.tabBarItem.badgeValue = @"99+";
    [self setChildViewController:home title:@"首页" tabbarImageName:@"tabbar_home_os7" tabbarSelImageName:@"tabbar_home_selected_os7"];
    
    
    //discover
    DiscoverTableViewController *discover = [[DiscoverTableViewController alloc]init];
     discover.tabBarItem.badgeValue = @"6";
    [self setChildViewController:discover title:@"广场" tabbarImageName:@"tabbar_discover_os7" tabbarSelImageName:@"tabbar_discover_selected_os7"];
   
    
    //Message
    MessageTableViewController *message = [[MessageTableViewController alloc]init];
    message.tabBarItem.badgeValue = @"66";
    [self setChildViewController:message title:@"消息" tabbarImageName:@"tabbar_message_center_os7" tabbarSelImageName:@"tabbar_message_center_selected_os7"];
    
    //Me
    MeTableViewController *me = [[MeTableViewController alloc]init];
    me.tabBarItem.badgeValue = @"new";
    [self setChildViewController:me title:@"我" tabbarImageName:@"tabbar_profile_os7" tabbarSelImageName:@"tabbar_profile_selected_os7"];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    Class UITabBarButtonClass = NSClassFromString(@"UITabBarButton");
    for (UIView *v in self.tabBar.subviews) {
        if ([v isKindOfClass:UITabBarButtonClass]) {
            [v removeFromSuperview];
        }
           
    }

}

- (void)setChildViewController:(UIViewController *)vc title:(NSString *)title tabbarImageName:(NSString *)imgName tabbarSelImageName:(NSString *)selImgName
{
    vc.title = title;
    [vc.tabBarItem setImage:[[UIImage imageNamed:imgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] ];
    [vc.tabBarItem setSelectedImage:[[UIImage imageNamed:selImgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
     BLNavigationViewController *Nav = [[BLNavigationViewController alloc]initWithRootViewController:vc];
    [self addChildViewController:Nav];
  
    [self.customTabBar addTabBarButtonWithItem:vc.tabBarItem];
}

- (void)onTabBarButtonClick:(NSInteger)index
{
    self.selectedIndex = index;
}

- (void)onCenterButtonClick
{
    SendStatuesViewController *sendStautes = [[SendStatuesViewController alloc]init];
    sendStautes.delegate = self;
    BLNavigationViewController *nav = [[BLNavigationViewController alloc]initWithRootViewController:sendStautes];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)sendStatuesIsSeccess:(BOOL)isSeccess
{
    if (isSeccess) {
      [[NSOperationQueue mainQueue]addOperationWithBlock:^{
          [MBProgressHUD showSuccess:@"发送成功！"];
      }];
        
    }else{
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
             [MBProgressHUD showError:@"发送失败！"];
        }];
       
    }
}
@end
